﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace blackhouse.BlackApart.Data {
    public abstract class ReservationServiceSaveTransaction {

        #region Fields

        protected int reservationId;
        protected int serviceId;
        protected double price { get { return this.lines.Sum(l => l.Quantity) * unitPrice; } }
        protected double unitPrice;
        protected string clientReview;
        protected List<TransactionLine> lines = new List<TransactionLine>();
        protected int saveVariant;

        #endregion

        #region Methods

        public void SetReservationServiceSaveTransaction(int reservationId, int serviceId, double unitPrice, string clientReview, int serviceVariant) {
            this.reservationId = reservationId;
            this.serviceId = serviceId;
            this.unitPrice = unitPrice;
            this.clientReview = clientReview;
            this.saveVariant = serviceVariant;
        }

        public void AddLine(DateTime forDate, int quantity, int apartmentId) {
            forDate = forDate.Date;
            TransactionLine line = this.lines.FirstOrDefault(l => l.ForDate == forDate);
            if (line.ForDate != DateTime.MinValue)
                this.lines.Remove(line);

            line = new TransactionLine();
            line.ForDate = forDate;
            line.ApartmentId = apartmentId;
            line.Quantity = quantity;
            this.lines.Add(line);
        }

        #endregion

        #region Inner Classes

        protected struct TransactionLine {
            public DateTime ForDate;
            public int Quantity;
            public int ApartmentId;
        }

        /// <summary>
        /// Zapisuje zamówienie do bazy.
        /// </summary>
        /// <returns>OrderId</returns>
        public abstract int Save();

        #endregion

    }
}
